﻿using Furion;
using Furion.DatabaseAccessor.Extensions;
using iemtm.WeChat.Core;
using iemtm.WeChat.Core.Managers;
using iemtm.WeChat.Web.Entry.Areas.Admin.Model;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace iemtm.WeChat.Web.Entry.Areas.Admin.Controllers
{
    [Area("Admin")]
    [Route("/[controller]/[action]")]
    public class AnnouceListController : Controller
    {
        private static string realDataUrl= App.Configuration["AppProgramConfig:realDataUrl"];
              
        private readonly IWorkManager _workManager;
        private readonly IUserManager _userManager;
        private readonly IGeneratorManager _generatorManager;
        private readonly IInfoManager _infoManager;

        public AnnouceListController(IWorkManager workManager
            , IUserManager userManager
             , IGeneratorManager generatorManager
             , IInfoManager infoManager
            )
        {
            _workManager = workManager;
            _userManager = userManager;
              _generatorManager = generatorManager;
              _infoManager = infoManager;
        }
        public ActionResult Index()
        {
            
            return View();
        }

        
        /// <summary>
        /// 工单详情页面
        /// </summary>
        /// <param name="annouceCode"></param>
        /// <returns></returns>
        public async Task<ActionResult> Detail(string annouceCode)
        {
            AnnouceDetailModel model = new AnnouceDetailModel();
            model.AnnounceList =await _infoManager.GetAnnFirstOrDefaultAsync(c => c.AnnounceCode == annouceCode && c.PrjCode == _workManager.CurrentUser.PrjCode);
            model.AnnounceList.AnnouncePerson = (await _userManager.CheckUserAsync(model.AnnounceList.AnnouncePerson))?.UserName ;
            model.AnnouceStatus = (await _infoManager.GetCommTypeFirstOrDefaultAsync(c => c.TabName == "AnnounceStatus" && c.DicValue == model.AnnounceList.AnnounceStatus.ToString())).DicType;

            var genrecordIs = string.Join(",",(await _infoManager.GetAnnGenRecordIdAsync(model.AnnounceList.AnnounceCode)));
            string sqlExec = $"declare @genrecordId varchar(100) set @genrecordId = '{genrecordIs}' exec USP_ComputeGenRecordValidGenFee @genrecordId";
            model.GenRecord = sqlExec.SqlQuery<GenRecord>(); 
            //处理带载和空载
            for (int i = 0; i < model.GenRecord.Count; i++)
            {
                if (!model.GenRecord[i].RunningPeriod.ToString().Contains("0.00"))
                {
                    TimeSpan t3 = model.GenRecord[i].GenStop.Value - model.GenRecord[i].GenStart.Value;//计算相差
                    model.GenRecord[i].EmptyloadPeriod =decimal.Parse(t3.TotalHours.ToString()) - model.GenRecord[i].RunningPeriod.Value;//空载时长等于2个差减去带载时长

                }
                else
                {
                    model.GenRecord[i].RunningPeriod = 0.00m;
                    model.GenRecord[i].EmptyloadPeriod = 0.00m;
                }
                //处理距离
                model.GenRecord[i].Distance = model.GenRecord[i].Distance == null ? 0 : model.GenRecord[i].Distance;
            }
            //处理油机名称
            if (model.AnnounceList.RegisteredGenset != null && model.AnnounceList.RegisteredGenset != "")
            {
                model.GensetNum = MemoryEntity.gBuffGen.All.Values.FirstOrDefault(g => g.GeneratorCode == model.AnnounceList.RegisteredGenset).GensetNum;
     
            }
            //合并发电量
            model.CountKWpower = model.GenRecord.Select(c => c.KWpower).ToArray().Sum();
            //合并发电时长
            model.CountPeriod = model.GenRecord.Select(c => c.RunningPeriod.Value).ToArray().Sum();
            //实时监控跳转路径
            ViewBag.realDataUrl = string.Format(realDataUrl, model.AnnounceList.RegisteredDtuId);
   
            return View(model);
        }


        /// <summary>
        /// 绑定发电机-签到
        /// </summary>
        /// <returns></returns>
        public ActionResult BinGenset()
        {
            return View();
        }

        /// <summary>
        /// 回单
        /// </summary>
        /// <returns></returns>
        public ActionResult ReplyGenset()
        {
            return View();
        }
        /// <summary>
        /// 更换油机
        /// </summary>
        /// <returns></returns>
        public ActionResult ChangeAnnGenset()
        {
            return View();
        }
        /// <summary>
        /// 添加油机
        /// </summary>
        /// <returns></returns>
        public ActionResult ReBinGenset()
        {
            return View();
        }
    }
}
